package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.behavior.dto.EntryDto;
import com.heima.behavior.dto.LikesBehaviorDto;
import com.heima.behavior.dto.UpdateArticleMessage;
import com.heima.behavior.entity.ApBehaviorEntry;
import com.heima.behavior.entity.ApLikesBehavior;
import com.heima.behavior.mapper.ApLikesBehaviorMapper;
import com.heima.behavior.service.IApBehaviorEntryService;
import com.heima.behavior.service.IApLikesBehaviorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.util.AppThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP点赞行为表 服务实现类
 * </p>
 *
 * @author wsj
 * @since 2022-03-17
 */
@Service
public class ApLikesBehaviorServiceImpl extends ServiceImpl<ApLikesBehaviorMapper, ApLikesBehavior> implements IApLikesBehaviorService {

    @Autowired
    private IApBehaviorEntryService entryService;
    @Value("${topic.hotArticleScoreTopic}")
    private String hotArticleScoreTopic;
    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;
    @Override
    public ResponseResult likesBehavior(LikesBehaviorDto dto) {
        //点赞或取消点赞
        //获取线程中的user
        User user = AppThreadLocalUtil.get();
        if (user==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //保存实体
        EntryDto entryDto = new EntryDto();
        entryDto.setEquipmentId(dto.getEquipmentId());
        entryDto.setUserId(user.getId());
        ApBehaviorEntry entry = entryService.saveEntry(entryDto);
        //保存APP点赞行为表
        //构建条件查询是否存在
        LambdaQueryWrapper<ApLikesBehavior> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ApLikesBehavior::getArticleId, dto.getArticleId());
        wrapper.eq(ApLikesBehavior::getEntryId, entry.getId());
        wrapper.eq(ApLikesBehavior::getType, dto.getType());
        ApLikesBehavior behavior = getOne(wrapper);
        if (behavior!=null) {
            //存在
            behavior.setOperation(dto.getOperation());
        }else {
            //不存在
            behavior = new ApLikesBehavior();
            behavior.setOperation(dto.getOperation());
            behavior.setArticleId(Long.parseLong(dto.getArticleId()));
            behavior.setCreatedTime(new Date());
            behavior.setEntryId(entry.getId());
            behavior.setType(dto.getType());
        }
        saveOrUpdate(behavior);
        //发送点赞消息到kafka
        if (dto.getType()==0) {
            UpdateArticleMessage message = new UpdateArticleMessage();
            message.setType(1);
            message.setArticleId(behavior.getArticleId());
            message.setAdd(dto.getOperation()==0?1:-1);
            String jsonString = JSON.toJSONString(message);
            kafkaTemplate.send(hotArticleScoreTopic,jsonString);
        }
        return ResponseResult.okResult();
    }
}
